<?php


namespace app\api\controller;


use think\Controller;
use think\Db;
use think\Exception;
use think\Validate;

class Community extends Controller
{

    /**
     * 首页推荐内容
     * @author zhixuedong
     * @date 2021/1/23/023
     */
    public function hotList(){
        $common = new Common();
        try{
            $content = "admin_content";
            $where['classify_id'] = 1;
            $where['hot'] = 1;
            $field = "id,thumb,title,desc";
            $list = $common->public_select($content,$where,$field,0,4)->order('sort desc,id desc')->select();

            $black['code'] = 0;
            $black['list'] = $list;
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

    /**
     * 获取景点介绍
     * @author zhixuedong
     * @date 2021/1/23/023
     */
    public function scenic(){
        $common = new Common();
        try{
            $content = "community_colony";
            $where['id'] = 1;
            $field = "id,name,content,images,thumb,desc,lng,lat";
            $data = $common->public_select($content,$where,$field)->find();

            $images = [];
            if(!empty($data['images'])){
                $images = explode(",",$data['images']);
            }
            $data['images'] = $images;
            $black['code'] = 0;
            $black['data'] = $data;
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

    /**
     * 获取景点列表
     * @author zhixuedong
     * @date 2021/1/23/023
     */
    public function scenicList(){
        $common = new Common();
        try{
            $content = "admin_content";
            $where['classify_id'] = $_POST['classify_id'];
            $field = "id,thumb,title,desc";
            $page = empty($_POST['page']) ? 1 : $_POST['page'];
            $limit = empty($_POST['limit']) ? 10 : $_POST['limit'];
            $start = ($page - 1) * $limit;
            $list = $common->public_select($content,$where,$field,$start,$limit)->select();
            $count = $common->public_select($content,$where)->count("*");
            $black['code'] = 0;
            $black['list'] = $list;
            $black['count'] = $count;
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

    /**
     * 获取景点详细信息
     * @author zhixuedong
     * @date 2021/1/23/023
     */
    public function scenicDetaqil(){
        $common = new Common();
        try{
            $content = "admin_content";
            $where['id'] = $_POST['id'];
            $field = "id,thumb,title,desc,content,images,address";
            $data = $common->public_select($content,$where,$field)->find();
            $images = [];
            if(!empty($data['images'])){
                $images = explode(",",$data['images']);
            }
            $data['images'] = $images;
            $black['code'] = 0;
            $black['data'] = $data;
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }


    /**
     * 意见反馈
     * @return false|string
     * @author zhixuedong
     * @date 2021/1/31/031
     */
    public function message(){
        $common = new Common();
        $userid = $common->common();
        try{
            $where['id'] = $userid;
            $fans_id = Db::name("admin_fans")->where($where)->value("id");
            if(empty($fans_id)){
                throw new Exception("粉丝信息有误");
            }
            $rule = [
                ["classify_id", "require", "栏目id不能为空"],
                ["title", "require", "标题不能为空"],
                ["name", "require", "姓名不能为空"],
                ["mobile", "require", "手机号不能为空"],
            ];
            $validate = new Validate($rule);
            $data = $this->request->post();
            if (!$validate->check($data)) {
                throw new Exception($validate->getError());
            }
            if(!empty($data['mobile'])){
                if(!$common->isMobile($data['mobile'])){
                    throw new Exception("手机号格式有误");
                }
            }
            //获取栏目是否正确
            $classify = Db::name("community_article_classify")->where(['id'=>$data['classify_id']])->find();
            if(empty($classify)){
                throw new Exception("非法操作");
            }
            if(!empty($data['image'])){
                $images = $data['image'];
                $data['image'] = implode($images,",");
            }
            $data['fans_id'] = $fans_id;
            $data['addtime'] = time();
            //市民留言
            $id = Db::name("community_message")->insertGetId($data);
            //记录操作日志
            $log['remark'] = "用户在栏目【".$classify['name']."】发布留言信息,标题【".$data['title']."】关联id【".$id."】";
            $log['operator'] = $userid;
            $log['operator_id'] = $fans_id;
            $common->addLog($log);

            $black['code'] = 0;
            $black['data'] = '操作成功';
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }


    /**
     * 我的意见反馈列表
     * @author zhixuedong
     * @date 2021/1/31/031
     */
    public function messageList(){
        $common = new Common();
        $userid = $common->common();
        try{
            $page = empty($_POST['page']) ? 1 : $_POST['page'];
            $limit = empty($_POST['limit']) ? 10 : $_POST['limit'];
            $start = ($page - 1) * $limit;
            $where['fans_id'] = $userid;
            $list = Db::name("community_message")->where($where)->field("id,title,content,is_reply,reply,name,mobile")->limit($start,$limit)->select();
            foreach ($list as $k=>$v){
                $list[$k]['is_reply'] = ($v['is_reply']==1)?"未回复":"已回复";
            }
            $black['code'] = 0;
            $black['list'] = $list;
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }


}